//
//  TTLookProgressViewController.m
//  TaoTuo
//
//  Created by admin on 2019/12/5.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTLookProgressViewController.h"
#import "TTLookProgressViewCell.h"
#import "LLInputAlertView.h"
#import <UIView+TYAlertView.h>

static NSString *cellID = @"cell";

@interface TTLookProgressViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
///订单号
@property (nonatomic, copy) NSString *orderNum;

/// 回收编号
@property (nonatomic, copy) NSString *reclaim_InformationId;

@end

@implementation TTLookProgressViewController

- (instancetype)initWithRecyclingNum:(NSString *)recyclingNum
{
    self = [super init];
    if (self) {
        self.reclaim_InformationId = recyclingNum;
    }
    return self;
}

- (instancetype)initWithOrderNum:(NSString *)orderNum
{
    self = [super init];
    if (self) {
        self.orderNum = orderNum;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"进度详情";
    
    [self getNetworkData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTLookProgressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (void)getNetworkData
{
    NSDictionary *dic = @{
        @"param.orderNo" : self.orderNum,
        @"param.pageIndex" : @1,
        @"param.pageSize" : @50,
        @"param.reclaim_InformationId" : self.reclaim_InformationId
    };
    
    NSString *urlStr;
    if (self.orderNum) {
        urlStr = lookOrderProgress_api; // 订单进度
    } else {
        urlStr = recyclingRecord_api;   // 回收进度
    }
    
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:urlStr parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in responseObject) {
            TTLookProgressModel *model = [TTLookProgressModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        
        TTLookProgressModel *firstModel = self.dataArray.firstObject;
        firstModel.selected = YES;
        
        TTLookProgressModel *lastModel = self.dataArray.lastObject;
        lastModel.hiddenBottomLine = YES;
        
        [self.tableView reloadData];
        
        if (self.orderNum) {
            [self setupFooterView];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {

    } showLoadingInView:self.view];
    
    
}

/// 修改备注
- (void)updateRemark:(NSString *)remark
{
    NSString *content = remark.length > 0 ? remark : @" ";
    NSDictionary *dic = @{
        @"param.wholesalerName" : [TTLoginSingle sharedSingle].loginModel.RealName,
        @"param.orderNo" : self.orderNum,
        @"param.orderRemark" : content
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:addReMark_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        [self.view makeToast:@"添加备注成功"];
        [self getNetworkData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {

    } showLoadingInView:self.view];
}

// 修改备注
- (void)changeRemark
{
    @weakify(self)
    LLInputAlertView *inputView = [LLInputAlertView inputViewWithTitle:@"添加备注" content:@"" maxTextCount:200 confirmBlock:^(NSString * _Nullable content) {
        @strongify(self)
        if (content.length > 0) {
            [self updateRemark:content];
        }
    }];
    [inputView showInWindowWithBackgoundTapDismissEnable:YES];
}

- (void)setupFooterView
{
    UIButton *btn = [UIButton createButtonWithTitle:@"添加备注" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainColor target:self action:@selector(changeRemark)];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedRowHeight = 60;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTLookProgressViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
